package com.joysuch.wwyt.workflow.service.impl;

import com.joysuch.wwyt.core.bean.AuthorityBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.workflow.WorkFlowConstants;
import com.joysuch.wwyt.workflow.bean.WorkFlowDocBean;
import com.joysuch.wwyt.workflow.bean.WorkFlowOptionBean;
import com.joysuch.wwyt.workflow.bean.WorkFlowOptionTypeBean;
import com.joysuch.wwyt.workflow.entity.WorkFlowDoc;
import com.joysuch.wwyt.workflow.entity.WorkFlowOption;
import com.joysuch.wwyt.workflow.entity.WorkFlowOptionType;
import com.joysuch.wwyt.workflow.repository.WorkFlowDocDao;
import com.joysuch.wwyt.workflow.repository.WorkFlowOptionTypeDao;
import com.joysuch.wwyt.workflow.service.WorkFlowDocService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class WorkFlowDocServiceImpl implements WorkFlowDocService {

    @Autowired
    private WorkFlowDocDao workFlowDocDao;

    @Override
    public Page<WorkFlowDoc> getDocList(Pageable pageable){
        return workFlowDocDao.findAll(pageable);
    }

    @Override
    public ResultBean save(WorkFlowDocBean bean){
        WorkFlowDoc workFlowDoc = null;
        WorkFlowDoc dbEntity = null;
        if(bean.getId() == null || bean.getId() ==0){
            //Code 不能重复
            dbEntity = workFlowDocDao.findByDocCode(bean.getDocCode());
            if(dbEntity != null){
                return ResultBean.wrapErrorResultDefault("文档清单编码已经存在.");
            }
            workFlowDoc = new WorkFlowDoc();
        }else{
            dbEntity = workFlowDocDao.findByDocCodeAndIdIsNot(bean.getDocCode(), bean.getId());
            if(dbEntity != null){
                return ResultBean.wrapErrorResultDefault("文档清单编码已经存在.");
            }
            workFlowDoc = workFlowDocDao.findById(bean.getId()).get();
        }
         workFlowDoc.setDocCode(bean.getDocCode());
         workFlowDoc.setDocName(bean.getDocName());
        return  ResultBean.success(workFlowDocDao.save(workFlowDoc));
    }

    @Override
    public void delete(Long id) {
        workFlowDocDao.deleteById(id);
    }

    @Override
    public WorkFlowDoc findById(Long id) {
      return  workFlowDocDao.findById(id).get();
    }

    @Override
    public  List<AuthorityBean> getDocMenuList(){
        List<AuthorityBean>  list = new ArrayList<>();
        List<WorkFlowDoc> docList = workFlowDocDao.findAllOrderByShowOrder();
        if(docList != null){
            docList.stream().forEach(item->{
                AuthorityBean au = new AuthorityBean();
                au.setId(item.getId());
                au.setLabel(item.getDocName());
                au.setOrder(item.getShowOrder());
                au.setCode(item.getDocCode());
                au.setName(WorkFlowConstants.WORK_FLOW_DOC_MENU_NAME);
                au.setType(WorkFlowConstants.WORK_FLOW_SYSTEM_MENU_TYPE);
                au.setUrl(WorkFlowConstants.WORK_FLOW_DOC_MENU_URL);
                list.add(au);
            });
        }

        return list;
    }


}
